package com.design.graduation.asdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.design.graduation.asdemo.R;
import com.design.graduation.asdemo.base.ActionBarActivity;
import com.design.graduation.asdemo.manager.UserManager;
import com.design.graduation.asdemo.model.Test;
import com.design.graduation.asdemo.service.DBService;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.design.graduation.asdemo.network.HSFObserver;
import com.design.graduation.asdemo.network.model.LoginResult;
import com.design.graduation.asdemo.utils.ToastUtil;

import rx.Observer;

/**
 * Created by lijinyuan on 16/2/19.
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends ActionBarActivity {


    @ViewById(R.id.login_phone_no)
    EditText editNo;

    @ViewById(R.id.login_code)
    EditText editPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DBService.insertOrReplaceTest(new Test(Long.valueOf(20), "ss", "dd"));
    }

    @AfterViews
    protected void init() {
        setTheme(R.style.AppTheme);
        initNormalActionBar();
        setTitle("登陆");
    }


    @Click(R.id.login_button)
    void login() {
        Long id=Long.parseLong(editNo.getText().toString());

        String password = editPassword.getText().toString();

        UserManager.RxLogin(id, password, new HSFObserver<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                ToastUtil.showLongToast(loginResult.getMessage());
                if (loginResult.getStatus()==200){
                    Intent intent=new Intent(LoginActivity.this,PersonalActivity_.class);
                    startActivity(intent);
                }
            }
        });

    }



    @Click(R.id.register_button)
    void register(){
        Long id=Long.parseLong(editNo.getText().toString());
        String password = editPassword.getText().toString();
        UserManager.RxRegister(id, password, new HSFObserver<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                ToastUtil.showLongToast(loginResult.getMessage());
                if (loginResult.getStatus() == 200) {
                    Intent intent = new Intent(LoginActivity.this, PersonalActivity_.class);
                    startActivity(intent);
                }
            }
        });


    }


    @Override
    public void onLeftMenuClicked(View view) {

    }


}
